﻿SmitearWebGL.Core.Math.Vector2 = Class.extend(
{
	/******** constructor ********/
	
	init : function( x, y )
	{
		this.x = x == undefined ? 0 : x;
		this.y = y == undefined ? 0 : y;
		
		this._registClass( "SmitearWebGL.Core.Math.Vector2" );
	}
	,
	reset : function( x, y )
	{
		this.x = x == undefined ? 0 : x;
		this.y = y == undefined ? 0 : y;
	}
	,
	zero : function()
	{
		this.x = this.y = 0.0;
	}
	,
	clone : function()
	{
		return $V2( this.x, this.y );
	}
	,
	copy : function( v )
	{
		this.x = v.x;
		this.y = v.y;
	}
	,
	multiply : function( v )
	{
		this.x *= v.x;
		this.y *= v.y;
	}
	,
	add : function( v )
	{
		this.x += v.x;
		this.y += v.y;
	}
	,
	subtract : function( v )
	{
		this.x -= v.x;
		this.y -= v.y;
	}
	,
	opposite : function()
	{
		this.x = - this.x;
		this.y = - this.y;
	}
	,
	normalize : function()
	{
		var mod = this.modulo();

		if( mod != 0 && mod != 1 )
		{
			mod = 1 / mod;
			
			this.x *= mod;
			this.y *= mod;
		}
	}
	,
	modulo : function()
	{
		return Math.sqrt( this.moduloSquared() );
	}
	,
	moduloSquared : function()
	{
		return ( this.x * this.x + this.y * this.y );
	}
	,
	dot : function( v )
	{
		return ( this.x * v.x + this.y * v.y  );
	}
	,
	//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * trace * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	toString : function()
	{
		return "SmitearWebGL.Core.Math.Vector2(" + this.x + ", " + this.y + ")";
	}
	,
	flatten : function ()
	{
		return [this.x, this.y];
	}
	,
	isEqualTo : function( v )
	{
		return ( this.x == v.x && this.y == v.y );
	}
}
);

SmitearWebGL.Core.Math.Vector2.create = function( e0, e1 )
{
	return new SmitearWebGL.Core.Math.Vector2( e0, e1 );
}

SmitearWebGL.Core.Math.Vector2.ZERO = function()
{
	return this.create( 0, 0 );
}

//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * OPERATIONS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

SmitearWebGL.Core.Math.Vector2.normal = function( v )
{
	var vn = v.clone();
	vn.normalize();

	return vn;
}

SmitearWebGL.Core.Math.Vector2.add = function( v, w )
{
	return this.create( v.x + w.x, v.y + w.y );
}

SmitearWebGL.Core.Math.Vector2.sub = function( v, w )
{
	return this.create( v.x - w.x, v.y - w.y );
}

var $V2 = SmitearWebGL.Core.Math.Vector2.create;